package cn.web.auction.controller;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.web.auction.util.Result;
import cn.web.auction.pojo.User;
import cn.web.auction.service.UserService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


@Controller
public class UserController {
	@Autowired
	private UserService userService;
	@CrossOrigin
	@ResponseBody
	@RequestMapping(value="doLogin",method = RequestMethod.POST)
	public Object doLogin(String userid,String userpassword) {
		//2.查询数据库
		System.out.println("running"+userid+""+userpassword);
				User user = userService.login(userid, userpassword);
				if(user != null) {//登录成功
					JSONObject jsonstr = JSONObject.fromObject(user);
					return Result.success(jsonstr);
				}
				else {
					
					return "失败";
				}
	}
	
	@CrossOrigin
	@ResponseBody
	@RequestMapping("/register")
	public Object register(User user) {
		userService.register(user);
		JSONArray jsonstr = JSONArray.fromObject(user);
		return Result.success(jsonstr);
	}

}
